package pa165.dominatingspecies.frontend.user;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.UserType;
import pa165.dominating.species.service.UserService;
import pa165.dominating.species.tos.UserTO;
import pa165.dominatingspecies.frontend.element.EnumChoiceRenderer;
import pa165.dominatingspecies.frontend.element.EnumDropDown;
import pa165.dominatingspecies.frontend.pages.BasePage;

/**
 * @author Tomas Kalla
 */
@PreAuthorize("hasRole('ADMIN')")
public class CreateUserPage extends BasePage {

    @SpringBean(name = "userService")
    private UserService userService;
    private EnumDropDown<UserType> userTypeDropDown;
    private UserTO userTO = new UserTO();
    private UserType selected = UserType.WORKER;

    public CreateUserPage() {
        Form<UserTO> createForm = new Form<>("createUserForm", new CompoundPropertyModel<>(userTO));
        add(createForm);
        createForm.add(new Label("nameLabel", new StringResourceModel("uName", this, null)));
        createForm.add(new Label("usernameLabel", new StringResourceModel("uLogin", this, null)));
        createForm.add(new Label("passwordLabel", new StringResourceModel("uPassword", this, null)));
        createForm.add(new Label("userTypeLabel", new StringResourceModel("uUserType", this, null)));

        createForm.add(new TextField<String>("name"));
        createForm.add(new TextField<String>("login"));
        createForm.add(new TextField<String>("password"));

        userTypeDropDown = new EnumDropDown<>("type", new PropertyModel<UserType>(this, "selected"), new EnumChoiceRenderer<UserType>(this));
        createForm.add(userTypeDropDown);

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                boolean errorsFound = false;
                if (userTO.getName() == null) {
                    getSession().warn(new StringResourceModel("userNameEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (userTO.getLogin() == null) {
                    getSession().warn(new StringResourceModel("userLoginEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (userTO.getPassword() == null) {
                    getSession().warn(new StringResourceModel("userPasswordEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (userTypeDropDown.getModelObject() == null) {
                    getSession().warn(new StringResourceModel("userTypeNotSelected", this, null).getString());
                    errorsFound = true;
                }
                if(userTO.getPassword() != null){
                  userTO.setPassword(DigestUtils.shaHex(userTO.getPassword()));
                }
                
                if (!errorsFound) {
                    boolean thrown = false;
                    try {
                        userTO.setType(userTypeDropDown.getModelObject());
                        userService.createUser(userTO);
                    } catch (DataAccessException e) {
                        getSession().warn(new StringResourceModel("userExists", this, null).getString());
                        System.out.println(e.getMessage());
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("userSucc", this, null).getString());
                        setResponsePage(UserPage.class);
                    }
                }
            }
        };
        createForm.add(submitButton);

        Link<BasePage> goBack = new Link<BasePage>("goBack") {
            @Override
            public void onClick() {
                setResponsePage(new UserPage());
            }
        };
        createForm.add(goBack);
    }
}
